package cn.shyouchi.wuguanjia.android.ui.trainstatistic;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.shyouchi.wuguanjia.android.R;
import cn.shyouchi.wuguanjia.android.api.train.TrainApi;
import cn.shyouchi.wuguanjia.android.base.BaseActivity;
import cn.shyouchi.wuguanjia.android.model.BaseRequestResult;
import cn.shyouchi.wuguanjia.android.subscribers.ProgressSubscriber;
import cn.shyouchi.wuguanjia.android.widget.TopBar;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class TrainStatisticActivity extends BaseActivity {


    TrainApi mTrainApi;
    @BindView(R.id.topBar)
    TopBar mTopBar;
    @BindView(R.id.rv_trains)
    RecyclerView mRvTrains;

    private String projectId;

    TrainStatisticAdapter mTrainStatisticAdapter;

    List<TrainStatistEntity> mTrainStatistEntities = new ArrayList<>();


    @Override
    public int initContentView() {
        return R.layout.activity_train_statistic;
    }

    @Override
    public void initInjector() {
        mTrainApi = new TrainApi();

        projectId = getIntent().getStringExtra("projectid");


    }

    @Override
    public void initUiAndListener() {
        ButterKnife.bind(this);
        mTopBar.setTitle(getTitle().toString());
        mTopBar.setLeftIcon(R.mipmap.ic_back);
        setSupportActionBar(mTopBar.getToolbar());


        mTrainStatisticAdapter = new TrainStatisticAdapter(mTrainStatistEntities);
        mRvTrains.setAdapter(mTrainStatisticAdapter);

        mRvTrains.setLayoutManager(new LinearLayoutManager(mContext));


        fetchTrainStatistic();
    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isApplyStatusBarColor() {
        return true;
    }


    private void fetchTrainStatistic() {
        mTrainApi.getTrainStatistic(projectId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber<BaseRequestResult<List<TrainStatistEntity>>>(mContext) {
                    @Override
                    public void onNext(BaseRequestResult<List<TrainStatistEntity>> Result) {
                        mTrainStatistEntities.clear();
                        mTrainStatistEntities.addAll(Result.getResultMessage());
                        mTrainStatisticAdapter.notifyDataSetChanged();
                    }
                });
    }

}
